import Vue from "vue";
import VueRouter from "vue-router";
import StuHomepage from "../views/student/StuHomepage.vue";
import SCLtest from "../views/student/SCLtest.vue";

import Testdisplay from "../components/test/Testsdisplay.vue";
import Resultquery from "../components/test/Resultqury.vue";
import Resultdisplay from "../components/test/Resultdispaly.vue";
import SCLresult from "../views/student/SCLresult.vue";
import Start from "../views/Start.vue";
import Login from "../components/Login.vue";
// import Register from "../components/Register.vue";
import TeaHomepage from "../views/teacher/TeaHomepage.vue";
import studentAdd from "../views/class/studentAdd.vue";//添加单个学生
import checkClass from "../components/class/checkClass.vue"; //查看班级
import checkStudents from "../views/class/checkStudents.vue"; //查看学生详情
import CheckResults from "../views/teacher/CheckResults.vue";
import informationUpdate from '../views/student/informationUpdate.vue'//编辑学生
import managerHomepage from "../views/manager/managerHome";
import manageTeacher from "../components/manager/manageTeacher";
// !!! 这里的change是修改密码界面的组件，代替了之前的Modpsw  -- gzy 2022.1.19  !!!
import change from "../components/change.vue";
import Notfound from "../views/Notfound.vue";

import store from "../store";
// const home = import('../views/home.vue')//？？？

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/start/login",
    },
    {
      path: "/start",
      component: Start,
      children: [
        {
          path: "login",
          component: Login,
        },
        // {
        //   path: "register",
        //   component: Register,
        // },
      ],
    },
    {
      path: "/stuhome",
      component: StuHomepage,
      redirect: "/stuhome/testdisplay",
      meta: { isAuth: true },
      children: [
        {
          path: "testdisplay",
          component: Testdisplay,
          meta: { isAuth: true },
        },
        {
          path: "resultqury",
          component: Resultquery,
          // meta: { isAuth: true },
          children: [
            {
              path: "resultdisplay",
              component: Resultdisplay,
              // meta: { isAuth: true },
              props($route) {
                return {
                  stu_id: $route.query.student_test_id,
                  back_path: $route.query.back_path,
                };
              },
            },
          ],
        },
        {
          path: "modpsw",
          component: change,
          // meta: { isAuth: true },
        },
      ],
    },
    {
      path: "/SCLtest",
      component: SCLtest,
      // meta: { isAuth: true },
    },
    {
      path: "/SCLresult",
      component: SCLresult,
      // meta: { isAuth: true },
      props($route) {
        return {
          stu_id: $route.query.stu_id,
          type: $route.query.type,
          result_id: $route.query.result_id,
          date: $route.query.date,
        };
      },
    },
    {
      path: "/teaHome",
      component: TeaHomepage,
      meta: { isAuth: true },
      redirect: "/teaHome/checkClass",
      children: [
        {
          path: "checkClass",
          component: checkClass,
          meta: { isAuth: true },
        },
        {
          path: "studentAdd",
          component: studentAdd,
          // meta: {isAuth: true }
        },
        {
          path: "modpsw",
          component: change,
          // meta: { isAuth: true },
        },
      ],
    },
    {
      path: "/checkStudents",
      component: checkStudents,
      // meta: { isAuth: true },
    },
    {
      path: "/checkResults",
      component: CheckResults,
      // meta: { isAuth: true },
      props($route) {
        return {
          stu_id: $route.query.student_test_id,
          class_id: $route.query.class_id,
        };
      },
    },
    {
      path: "/informationUpdate",
      component: informationUpdate,
      // meta: { isAuth: true },
      props($route) {
        return {
          stu_id: $route.query.student_test_id,
          class_id: $route.query.class_id,
        };
      },
    },
    {
      path: "/managerHome",
      component: managerHomepage,
      redirect: "/managerHome/manageTeacher",
      // meta: { isAuth: true },
      children: [
        {
          path: "manageTeacher",
          component: manageTeacher,
          // meta: { isAuth: true },
        },
        {
          path: "modpsw",
          component: change,
        },
      ],
    },
    {
      path: "*",
      component: Notfound,
    },
  ],
  mode: "history",
});

router.beforeEach((to, from, next) => {
  if (to.meta.isAuth) {
    if (
      window.sessionStorage.getItem("teaToken") &&
      to.path == "/teaHome/checkClass"
    ) {
      // console.log("准备去往老师页面");
      next();
    } else if (
      window.sessionStorage.getItem("stuToken") &&
      to.path == "/stuhome/testdisplay"
    ) {
      // console.log("准备去往学生页面");
      next();
    } else if (
      window.sessionStorage.getItem("managerToken") &&
      to.path == "/managerHome/manageTeacher"
    ) {
    } else {
      alert("您没有去权限，请先登录！");
      next("/");
    }
  } else {
    next();
  }
});

export default router;
